package com.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.impl.AttrAttrgroupRelationServiceImpl;
import com.gulimall.product.service.impl.AttrServiceImpl;
import com.gulimall.product.service.impl.ProductAttrValueServiceImpl;
import com.gulimall.product.vo.AttrVo;
import com.gulimall.product.vo.AttrVoRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrService;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrServiceImpl attrServiceImpl;

    @Resource
    private ProductAttrValueServiceImpl productAttrValueServiceImpl;
    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
//    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long cateLogId,@PathVariable("type")String type){

        PageUtils page = attrServiceImpl.queryPage(type,cateLogId,params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */

    @RequestMapping("/info/{attrId}")
//    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrVoRes attr = attrServiceImpl.queryAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 获取spu关联的规格属性
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getSpuAttr( @PathVariable("spuId") String spuId){
        List<ProductAttrValueEntity> productAttrValueList = productAttrValueServiceImpl.getBaseMapper().selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        return R.ok().put("data", productAttrValueList);
    }

    /**
     * 修改spu相关的属性
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr( @PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> productAttrValueList){
        productAttrValueServiceImpl.updateSpuAttr(spuId,productAttrValueList);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo){
        attrServiceImpl.saveDetail(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo){
        attrServiceImpl.updateDetail(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
        attrServiceImpl.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
